package com.kfm.wiki.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kfm.wiki.entity.Ebook;
import com.kfm.wiki.entity.EbookDTO;
import com.kfm.wiki.service.EbookService;
import com.kfm.wiki.mapper.EbookMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author 10854
* @description 针对表【ebook(电子书)】的数据库操作Service实现
* @createDate 2022-12-12 10:03:21
*/
@Service
public class EbookServiceImpl extends ServiceImpl<EbookMapper, Ebook>
    implements EbookService{


    @Override
    public PageInfo<Ebook> list(Integer current, Integer pageSize, Ebook ebook) {
        PageHelper.startPage(current, pageSize);
        // 创建搜索条件
        QueryWrapper<Ebook> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(ebook.getName())){
            queryWrapper.like("name", ebook.getName());
        }
        List<Ebook> list = super.list(queryWrapper);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<EbookDTO> listWithCategory(Integer current, Integer pageSize, Ebook ebook){
        PageHelper.startPage(current, pageSize);
        List<EbookDTO> list = getBaseMapper().selectAllWithCategory(ebook);
        return new PageInfo<>(list);
    }

    @Override
    public boolean save(Ebook ebook) {
        if (ObjectUtils.isEmpty(ebook.getId())){
            // 新增电子书
            // 设置 id
            ebook.setId(IdUtil.getSnowflakeNextId());
            // 保存
            return getBaseMapper().insert(ebook) > 0;
        } else{
            // 编辑信息
            return getBaseMapper().updateById(ebook) > 0;
        }

    }

    @Override
    public int updateEbookInfo() {
        return getBaseMapper().updateEbookInfo();
    }
}




